<?php

namespace App\Transformers;

use App\Models\FsUserHasIm;
use App\Models\Level;
use App\Models\FsUserPhone;
use App\Models\FsUserRemark;
use App\Models\Role;
use App\Models\FsUser;
use App\Models\User;
use League\Fractal\TransformerAbstract;
use Illuminate\Support\Facades\Auth;

class FsUserTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['fsUserPhones','fsUserRemarks','fsUserHasIm','fsUserTasks'];
    protected $hasPermission;

    public function __construct($model = '')
    {
        //此处判断权限，返回是否隐藏手机
        $user = Auth::user();
        $res = $user->hasPermissionTo('api.fsuser.phone.all');
        $this->hasPermission = $res;
        if(!empty($model)){
            $this->defaultIncludes = [$model];
        }
    }

    public function transform(FsUser $user)
    {
        return [
            'id' => $user->id,
            'username' => $user->username,
            'agent'=> $user->agent,
            'register_time' => $user->register_time,
            'balance' => $user->balance,
            'last_login_time' => $user->last_login_time,
            'last_charge_time' => $user->last_charge_time,
            'last_refund_time' => $user->last_refund_time,
            'ip' => $user->ip,
            'status' => $user->status,
            'lev' => $user->lev,
            'true_name' => $user->true_name,
            'true_id' => $user->true_id,
            'phone_num' => $this->hasPermission?$user->phone_num:hidePhone($user->phone_num),
            'qq' => $this->hasPermission?$user->qq:substr_name($user->qq),
            'mail' =>$this->hasPermission?$user->mail:substr_name($user->mail),
            'province' => $user->province,
            'city' => $user->city,
            'bank_name' => $user->bank_name,
            'bank_num' => $user->bank_num,
            'remark' => $user->remark,
            'site_id' => $user->site_id,
            'deposit_count' => $user->deposit_count,
            'deposit_max' => $user->deposit_max,
            'deposit_total' => $user->deposit_total,
            'withdrawal_count' => $user->withdrawal_count,
            'withdrawal_total'=> $user->withdrawal_total,
            'multiple' => $user->multiple,
            'profit' =>$user->profit,
            'call_status'=>$user->pivot->status??''
        ];
    }

    public function includeFsUserPhones(FsUser $fsUser)
    {
        $fsUserPhone = new FsUserPhone();
        return $this->item($fsUser->fsUserPhones->sortByDesc('created_at')->first()?$fsUser->fsUserPhones->sortByDesc('created_at')->first():$fsUserPhone, new FsUserPhoneTransformer());

    }

    public function includeFsUserRemarks(FsUser $fsUser)
    {
        $fsUserRemark = new FsUserRemark();
        return $this->item($fsUser->fsUserRemarks->sortByDesc('created_at')->first()?$fsUser->fsUserRemarks->sortByDesc('created_at')->first():$fsUserRemark, new FsUserRemarkTransformer());
    }

    public function includeFsUserHasIm(FsUser $fsUser){
        $fsUserHasIm = new FsUserHasIm();
        return $this->item($fsUser->fsUserHasIm?$fsUser->fsUserHasIm:$fsUserHasIm,new FsUserHasImTransformer());
    }

    public function includeFsUserTasks(FsUser $fsUser){
        if($fsUser->fsUserTasks->toArray()){
            return $this->nullOrItem($fsUser->fsUserTasks->sortByDesc('id')->first()->user, new UserTransformer());
        }else{
            return $this->nullOrItem(new User(), new UserSimpleTransformer());
        }
    }

    public function nullOrItem($data, $transformer)
    {
        if (is_null($data)) {
            return parent::null();
        } else {
            return parent::item($data, $transformer);
        }
    }
}